package com.junmo.wechat.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.junmo.common.enums.RemindCode;
import com.junmo.common.exception.RemindException;
import com.junmo.common.result.Result;
import com.junmo.common.util.StringUtils;
import com.junmo.wechat.pojo.dto.*;
import com.junmo.wechat.pojo.entity.SysMember;
import com.junmo.wechat.pojo.vo.MemberVO;
import com.junmo.wechat.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api(tags = "「移动端」会员管理")
@RestController
@RequestMapping("/app-api/v1/members")
@RequiredArgsConstructor
public class MemberController {

    private final IMemberService memberService;

    @ApiOperation(value = "注册会员")
    @PostMapping("/_register")
    public Result<Long> addMember(@RequestBody @NotNull MemberRegisterDTO member) {
        if (StringUtils.isEmpty(member.getMobile())){
            throw new RemindException(RemindCode.PHONE_NULL_REMIND);
        }
        if (StringUtils.isEmpty(member.getPassword()) || !member.getPassword().equals(member.getConfirmpassword())){
            throw new RemindException(RemindCode.PHONE_NULL_REMIND);
        }
        Long memberId = memberService.addMember(member);
        return Result.success(memberId);
    }

    @ApiOperation("忘记密码")
    @PostMapping("/_resetPassword")
    public Result resetPassword(@RequestBody @NotNull ResetPasswordDto dto) {
        boolean result = memberService.resetPassword(dto.getMobile(), dto.getOldpassword(), dto.getNewpassword(), dto.getConfirmpassword(), dto.getCaptcha());
        if (result) {
            return Result.success();
        }
        return Result.error(RemindCode.PASSWORD_RESERT_ERROR);
    }

    @ApiOperation(value = "获取会员个人信息")
    @GetMapping("/me")
    public Result<MemberVO> getCurrentMemberInfo() {
        MemberVO memberVO = memberService.getCurrentMemberInfo();
        return Result.success(memberVO);
    }

    @ApiOperation(value = "根据会员的memberId获取openid")
    @GetMapping("/{memberId}/openid")
    public Result<String> getMemberById(@ApiParam("会员ID") @PathVariable Long memberId) {
        SysMember member = memberService.getOne(
                new LambdaQueryWrapper<SysMember>()
                        .eq(SysMember::getId,memberId)
                        .select(SysMember::getOpenid)
        );
        String openid = member.getOpenid();
        return Result.success(openid);
    }

    @ApiOperation(value = "根据 openid 获取会员认证信息")
    @GetMapping("/openid/{openid}")
    public Result<MemberAuthInfoDTO> getByOpenid(@ApiParam("微信身份标识") @PathVariable String openid) {
        MemberAuthInfoDTO memberAuthInfo = memberService.getByOpenid(openid);
        return Result.success(memberAuthInfo);
    }

    @ApiOperation(value = "根据手机号获取会员认证信息")
    @GetMapping("/mobile/{mobile}")
    public Result<MemberAuthInfoDTO> getByMobile(@ApiParam("手机号码") @PathVariable String mobile) {
        MemberAuthInfoDTO memberAuthInfo = memberService.getByMobile(mobile);
        return Result.success(memberAuthInfo);
    }

}
